import {observable, action} from "mobx";
import {Form} from "weaver-mobile-page";
import {Bubble, Tools,Toast} from "weaver-mobile";
import {Browser} from "weaver-mobile-page";
import * as API from "../../api";

const getLabel = Tools.getLabel;
export default class formOperationsStore {
    @observable isLoading = true;
    @observable Toasting = false;
    @observable id;
    @observable source;
    @observable files=[]
    @observable viewtype;
    @observable urlParams;
    @observable taskInfo;
    @observable fieldinfo;
    @observable form = new Form.Store({route: "/FormOperations"});
    @observable browser = new Browser.Store({route: "/FormOperations", id: "search-history"});
    @action
    init = (urlParams) => {
        this.clearForm(this.form);
        this.getForm(urlParams);
    };


    @action
    getForm = (urlParams) => {
        this.isLoading = true;
        this.viewtype = urlParams.viewtype;
        let params = {viewtype: this.viewtype};
        if (urlParams["prjid"] !== undefined) {
            this.id = urlParams["prjid"];
            if (urlParams["parentid"] === undefined) {//获取项目可编辑表单
                this.source = "prjid";
                params = {...params, prjid: this.id};
                this.form = new Form.Store();
                API.getProjectForm(params).then(action(res => {
                    this.isLoading = false;
                    this.fieldinfo = res.fieldinfo;
                    this.form.initForm(this.fieldinfo, `/FormOperations`);
                }))
            } else {//任务
                if (urlParams["parentid"] === "prj") {//获取项目中的任务可编辑表单
                    this.source = "prj_child_task";
                    params = {...params, prjid: this.id, parentid: ""};
                } else {                              //获取任务中的子任务可编辑表单
                    this.source = "task_child_task";
                    params = {...params, prjid: this.id, parentid: urlParams["parentid"]};
                }
                API.getTaskForm(params).then(action(res => {
                    this.isLoading = false;
                    this.taskInfo = res;
                    this.fieldinfo = res.fieldinfo;
                    this.form = new Form.Store();
                    this.form.initForm(this.fieldinfo, `/FormOperations`);
                }))
            }

        } else if (urlParams["taskid"] !== undefined) {//获取任务可编辑表单
            this.id = urlParams["taskid"];
            this.source = "taskid";
            params = {...params, taskid: this.id};
            this.form = new Form.Store();
            API.getTaskForm(params).then(action(res => {
                this.isLoading = false;
                this.fieldinfo = res.fieldinfo;
                this.taskInfo = res;
                this.form.initForm(this.fieldinfo, `/FormOperations`);
            }))
        }
    };
    @action
    saveCard = (history) => {//新增和编辑
        if (this.form.doValidate().pass) {
            Toast.loading("Loading...", 10000);
            this.Toasting = true;
            const paramsAll = this.getParams(this.form);
            let params;
            if (this.source === "prjid") {
                params = {
                    ...paramsAll,
                    prjid: this.id,
                    method: this.viewtype
                }
                API.doProjectOperation(params).then(res => {
                    Toast.hide()
                    this.Toasting = false;

                    if (res.success) {
                        Bubble.success(getLabel('30700','操作成功') , 1);
                        history.go(-1);
                    } else {
                        Bubble.error(res.msg, 1);
                    }
                })
            } else if (this.source === "taskid" || this.source === "task_child_task") {
                params = {
                    ...paramsAll,
                    taskid: this.id,
                    method: this.viewtype
                }
                API.doTaskOperation(params).then(action(res => {
                    Toast.hide()
                    this.Toasting = false;
                    if (res.success) {
                        Bubble.success(getLabel('30700','操作成功') , 1);
                        history.go(-1);
                    } else {
                        Bubble.error(res.msg, 1);
                    }
                }))
            } else if (this.source === "prj_child_task") {
                params = {
                    ...paramsAll,
                    prjid: this.id,
                    method: this.viewtype
                }
                API.doTaskOperation(params).then(action(res => {
                    Toast.hide()
                    this.Toasting = false;
                    if (res.success) {
                        Bubble.success(getLabel('30700','操作成功') , 1);
                        history.go(-1);
                    } else {
                        Bubble.error(res.msg, 1);
                    }
                }))
            }
        }
    };
    @action
    getParams = (form) => {
        return form.formParams;
    };

    @action
    updateField = (form, key, value) => {
        form.updateField(key, value);
    };
    @action
    clearForm = (form) => {
        this.browser = new Browser.Store({route: "/FormOperations", id: "search-history"});
        form.resetFields();
    };
    @action
    groupToggle = (...arg) => {
        let title = arg[0];
        let status = arg[1];
        this.form.conditioninfo = this.form.conditioninfo.map(res => ({
            ...res,
            ...(res.title === title ? {show: status} : {})
        }));
    }
    @action
    setFormFields = (value) => {
        this.form.updateFields(value, false);    //true代表完全覆盖方式更新条件值
    }

    resetStage = (params, taskid) => {
        let conditions = this.fieldinfo;
        conditions.forEach(c => {
            c.items.forEach((field) => {
                if (field.domkey[0] === "stageid") {
                    field.viewAttr = params.viewAttr;
                    if (Number(params.viewAttr) === 1) {
                        API.getDateByWorkLongCmd({opttype: "getstage", taskid: taskid}).then(res => {
                            this.setFormFields({stageid: {value: res.stageid}})
                        })
                    } else {
                        this.setFormFields({stageid: {value: ""}});
                    }
                }
            })
        })

        this.fieldinfo = conditions;
    }
    @action
    setFiles=(v)=>{
        this.files = v;
    }
}
